package com.uniview.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uniview.dao.ServiceOrdersLogsDao;
import com.uniview.entity.po.ServiceOrdersLogs;
import com.uniview.service.ServiceOrdersLogsService;

@Service("serviceOrdersLogsService")
public class ServiceOrdersLogsServiceImpl implements ServiceOrdersLogsService {

	@Autowired
	private ServiceOrdersLogsDao serviceOrdersLogsDao;

	@Override
	public Integer saveOrderLogs(ServiceOrdersLogs sol) throws Exception {
		return (Integer) serviceOrdersLogsDao.save(sol);
	}

	@Override
	public List<ServiceOrdersLogs> findLogByOrderId(Integer orderId) throws Exception {
		return serviceOrdersLogsDao.findLogsByOrderId(orderId);
	}

	@Override
	public ServiceOrdersLogs findApplyLogsByCreator(Integer orderId) throws Exception {
		String sql = "select * from sd_orders_logs sd where sd.orderId=" + orderId + " and sd.stauts = 1 ";
		List<ServiceOrdersLogs> list = serviceOrdersLogsDao.findBySql(sql);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ServiceOrdersLogs findLastUrgeOrderLogByOrderId(Integer orderId) throws Exception {
		String sql = "select * from sd_orders_logs sd where sd.orderId=" + orderId
				+ " and sd.stauts = 0 and sd.handler='催办工单' order by sd.createTime desc";
		List<ServiceOrdersLogs> list = serviceOrdersLogsDao.findBySql(sql);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ServiceOrdersLogs findLatestLogByOrderId(Integer orderId) throws Exception {
		String sql = "select * from sd_orders_logs l1 where l1.createtime = (select  max(l2.createtime) from sd_orders_logs l2 where l2.orderid="
				+ orderId + ");";
		List<ServiceOrdersLogs> list = serviceOrdersLogsDao.findBySql(sql);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

}
